package longcaisuyun.longcai.com.fragment;

import android.app.Dialog;
import android.app.Fragment;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.http.AsyCallBack;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import longcaisuyun.longcai.com.net.PostDingDan;
import longcaisuyun.longcai.com.suyunbean.DingDanGlBean;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.DingdanjinxingAdapter;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.MyApplication;
import longcaisuyun.longcai.com.yiliubabalongcaisuyun.R;


/**
 * Created by Administrator on 2016/2/18.
 */
public class frag_main_dingdanguanli extends Fragment implements View.OnClickListener {
    TextView t_jinxingzhong;
    TextView t_yijieshu;
    public PullToRefreshListView pulllist;
    public ListView listView;
    String startpage = "1";
    String ispage = "";
    String nextpage = "";
    Button bt_zhuangtai;
    public static String type = "1";
    public static List<DingDanGlBean> list = new ArrayList<DingDanGlBean>();
    public static DingdanjinxingAdapter orderAdapter;
    private Dialog dialog;
    private View v;
    private Boolean isLast=true;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = View.inflate(getActivity(), R.layout.frag_main_dingdanguanli, null);
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) v);
        HttpPost(startpage);
        ViewInit(v);
        return v;
    }
    private void ViewInit(View v) {
        pulllist = (PullToRefreshListView) v.findViewById(R.id.pulllist);
        bt_zhuangtai = (Button) v.findViewById(R.id.bt_zhuangtai);
        t_jinxingzhong = (TextView) v.findViewById(R.id.t_jinxingzhong);
        t_jinxingzhong.setOnClickListener(this);
        t_yijieshu = (TextView) v.findViewById(R.id.t_yijieshu);
        t_yijieshu.setOnClickListener(this);
        pulllist.setScrollLoadEnabled(false);
        pulllist.setPullLoadEnabled(true);

        pulllist.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                isLast = true;
                HttpPost(startpage);
            }

            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pulllist.onPullUpRefreshComplete();
                if (!ispage.equals("0")) {
                    isLast = false;
                    HttpPostNo(nextpage);
                } else {
                    Toast.makeText(getActivity(), "已加载完毕", Toast.LENGTH_SHORT).show();
                }
            }
        });
        listView = pulllist.getRefreshableView();
    }


    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.t_jinxingzhong:
                isLast=true;
                switchid(1);
                break;
            case R.id.t_yijieshu:
                isLast=true;
                switchid(2);
                break;
        }
    }

    public void switchid(int m) {
        switch (m) {
            case 1:
                t_jinxingzhong.setTextColor(Color.parseColor("#ffffff"));
                t_jinxingzhong.setBackgroundColor(Color.parseColor("#e88837"));
                t_yijieshu.setTextColor(Color.parseColor("#5f5f5f"));
                t_yijieshu.setBackgroundColor(Color.parseColor("#f0f0f0"));
                type = "1";
                HttpPost(startpage);
                break;
            case 2:

                t_yijieshu.setTextColor(Color.parseColor("#ffffff"));
                t_yijieshu.setBackgroundColor(Color.parseColor("#e88837"));
                t_jinxingzhong.setTextColor(Color.parseColor("#5f5f5f"));
                t_jinxingzhong.setBackgroundColor(Color.parseColor("#f0f0f0"));
                type = "2";
                HttpPost(startpage);
                break;
        }
    }

    public void HttpPost(String page) {
        new PostDingDan(MyApplication.myPreferences.readUid(), type, page, new AsyCallBack<PostDingDan.Info>() {
            @Override
            public void onSuccess(String toast, int ty, PostDingDan.Info info) throws Exception {
                super.onSuccess(toast, ty, info);
                if (info.message.equals("0")) {
                    Toast.makeText(getActivity(), "获取数据失败，请重新尝试", Toast.LENGTH_SHORT).show();
                } else {
//                    if (type.equals("1")){
//                        Toast.makeText(getActivity(), "进行中", Toast.LENGTH_SHORT).show();
//                    }else{
//                        Toast.makeText(getActivity(), "已结束", Toast.LENGTH_SHORT).show();
//                    }
                    nextpage = info.nextpage;
                    ispage = info.ispage;
                    addlist(info.jsonArray);
                    orderAdapter = new DingdanjinxingAdapter(getActivity(), list, type, handler);
                    listView.setAdapter(orderAdapter);
                }
                pulllist.onPullDownRefreshComplete();
                pulllist.onPullUpRefreshComplete();
            }

            @Override
            public void onFail(String toast, int ty) throws Exception {
                super.onFail(toast, ty);
                pulllist.onPullDownRefreshComplete();
                pulllist.onPullUpRefreshComplete();
                orderAdapter.notifyDataSetChanged(null);
                Toast.makeText(getActivity(), "获取数据失败，请重新尝试", Toast.LENGTH_SHORT).show();
            }
        }).execute(getActivity());

    }
    public void HttpPostNo(String page) {
        new PostDingDan(MyApplication.myPreferences.readUid(), type, page, new AsyCallBack<PostDingDan.Info>() {
            @Override
            public void onSuccess(String toast, int ty, PostDingDan.Info info) throws Exception {
                super.onSuccess(toast, ty, info);
                if (info.message.equals("0")) {
                    Toast.makeText(getActivity(), "获取数据失败，请重新尝试", Toast.LENGTH_SHORT).show();
                } else {
                    nextpage = info.nextpage;
                    ispage = info.ispage;
                    addlist(info.jsonArray);
                    orderAdapter.notifyDataSetChanged(list);
                }
                pulllist.onPullDownRefreshComplete();
                pulllist.onPullUpRefreshComplete();
            }

            @Override
            public void onFail(String toast, int ty) throws Exception {
                super.onFail(toast, ty);
                pulllist.onPullDownRefreshComplete();
                pulllist.onPullUpRefreshComplete();
                orderAdapter.notifyDataSetChanged(null);
                Toast.makeText(getActivity(), "获取数据失败，请重新尝试", Toast.LENGTH_SHORT).show();
            }
        }).execute(getActivity());

    }
    public void addlist(JSONArray jsonArray) {
        if (isLast){
            list.clear();
        }
        if (jsonArray.length() > 0) {
            for (int j = 0; j < jsonArray.length(); j++) {
                DingDanGlBean dingDanGlBean = new DingDanGlBean();
                try {
                    dingDanGlBean.setId(jsonArray.getJSONObject(j).getString("id"));
                    dingDanGlBean.setDatetype(jsonArray.getJSONObject(j).getString("datetype"));
                    dingDanGlBean.setUsetime(jsonArray.getJSONObject(j).getString("usetime"));
                    dingDanGlBean.setCheckinfo(jsonArray.getJSONObject(j).getString("checkinfo"));
                    dingDanGlBean.setStart(jsonArray.getJSONObject(j).getJSONArray("addresslist").getJSONObject(0).getString("address"));
                    dingDanGlBean.setEnd(jsonArray.getJSONObject(j).getJSONArray("addresslist").getJSONObject(jsonArray.getJSONObject(j).getJSONArray("addresslist").length() - 1).getString("address"));
                    dingDanGlBean.setAccount(jsonArray.getJSONObject(j).getString("account"));
                    dingDanGlBean.setPaystatue(jsonArray.getJSONObject(j).getString("paystatue"));
                    list.add(dingDanGlBean);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        } else {
//            Toast.makeText(getActivity(), "暂无数据", Toast.LENGTH_SHORT).show();
        }

    }

    public void diag() {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.item_caozuo_phone, null);
        TextView tv_title = (TextView) layout.findViewById(R.id.textView28);
        tv_title.setText("是否删除该条订单");
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) layout);
        dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new BitmapDrawable());
        dialog.setCancelable(false);
        dialog.show();
        dialog.setContentView(layout);
        TextView tv_phone = (TextView) layout.findViewById(R.id.t_phone);
        tv_phone.setText("删除");
        TextView tv_cancle = (TextView) layout.findViewById(R.id.t_cancle);
        tv_phone.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tv_cancle.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                dialog.dismiss();

            }
        });
    }

    Handler handler = new Handler() {

        @Override
        public void dispatchMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    HttpPost("1");
                    break;
                case 1:
                    HttpPost("1");
                    break;
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        HttpPost("1");
    }

    //刷新页面
    public void shuaXin(){
        Log.e("揍我了","走开了");
        HttpPost(startpage);
    }
}
